package cn.spike.rockcc.service.impl;

import cn.spike.rockcc.domain.CourseType;
import cn.spike.rockcc.dto.CrumbsDto;
import cn.spike.rockcc.mapper.CourseTypeMapper;
import cn.spike.rockcc.service.ICourseTypeService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程目录 服务实现类
 * </p>
 *
 * @author spike
 * @since 2023-03-11
 */
@Service
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeMapper, CourseType> implements ICourseTypeService {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;


    @Override
    //添加缓存
    @Cacheable(cacheNames = "treeDate",key = "'ALL'")
    public List<CourseType> treeData() {
        return getTreeForDb();
    }

    /**
     * 导航面包屑
     * @param id
     * @return
     */
    @Override
    //添加缓存
    @Cacheable(cacheNames = "crumbs",key = "'ALL'")
    public List<CrumbsDto> crumbs(Long id) {
        List<CrumbsDto> crumbsDtos = new ArrayList<>();
        CourseType courseType = super.selectById(id);
        String[] paths = courseType.getPath().split("\\.");
        for (String path : paths) {
            CrumbsDto crumbsDto = new CrumbsDto();
            CourseType ownerProductType = super.selectById(Long.parseLong(path));
            crumbsDto.setOwnerProductType(ownerProductType);
            if(ownerProductType.getPid()!=null){
                Wrapper<CourseType> wrapper = new EntityWrapper<>();
                wrapper.eq("pid",ownerProductType.getPid());
                crumbsDto.setOtherProductTypes(super.selectList(wrapper));
            }
            crumbsDtos.add(crumbsDto);
        }
        return crumbsDtos;
    }


    private List<CourseType> getTreeForDb(){
        //System.out.println("从数据库获取");
        //获取所有的课程类型
        List<CourseType> courseTypes = super.selectList(null);
        //转换成map - id作为key,课程类型作为value
        Map<Long, CourseType> map = courseTypes.stream().collect(Collectors.toMap(CourseType::getId, CourseType -> CourseType));
        List<CourseType> treeData = new ArrayList<>();
        for (CourseType courseType : courseTypes) {
            if(courseType.getPid()==null||courseType.getPid()==0)treeData.add(courseType);
            else {
                map.get(courseType.getPid()).getChildren().add(courseType);
            }
        }
        //保存到redis中
        //redisTemplate.opsForValue().set("courseTypeTree",treeData);
        return treeData;
    }

    @Override
    //剔除缓存
    @CacheEvict(cacheNames = {"treeDate","crumbs"},key = "'ALL'")
    public boolean insert(CourseType entity) {
        return super.insert(entity);
    }

    @Override
    //剔除缓存
    @CacheEvict(cacheNames = {"treeDate","crumbs"},key = "'ALL'")
    public boolean deleteById(Serializable id) {
        return super.deleteById(id);
    }

    @Override
    //剔除缓存
    @CacheEvict(cacheNames = {"treeDate","crumbs"},key = "'ALL'")
    public boolean updateById(CourseType entity) {
        return super.updateById(entity);
    }
}
